/**
 * 右键菜单扩展类
 * @author	eidiot
 * ibio 整理、修改
 * 2009-8-5 13:52
 */
package com.ibio8.utils {
	import flash.events.ContextMenuEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuBuiltInItems;
	import flash.ui.ContextMenuItem;
	/**
	 * 右键菜单扩展类。
	 * 
	 * <p>
	 * 右键菜单扩展类，可以同时添加多个菜单。
	 * </p>
	 * <b>此类原作者 eidiot。</b>
	 */
	public class ContextMenuPlus {
		/** @private **/
		protected var m_contextMenu:ContextMenu;
		
		/**
		 * 构造函数
		 * @param removeAll			是否禁用所有默认菜单
		 * @param listener			自定义菜单事件句柄
		 * @param ...customItems	自定义标签，可以是一个数组
		 * @usage
		 * target.contextMenu = new ContextMenuPlus().contextMenu;
		 */
		public function ContextMenuPlus(removeAll:Boolean = true, listener:Function = null, ...customItems) {
			m_contextMenu = new ContextMenu();
			//删除所有
			if(removeAll){
				removeItems();
			}
			//如果菜单总数 > 0，则添加
			if (customItems.length > 0) {
				addCustomItems(listener, customItems);
			}
		}
		
		/**
		 * 当前的菜单实例
		 */
		public function get contextMenu():ContextMenu {
			return m_contextMenu;
		}
		
		/**
		 * 删除默认菜单
		 * @param ...excepts	需要保留的默认菜单(标签数组)
		 */
		public function removeItems(...excepts):void {
			m_contextMenu.hideBuiltInItems();
			if(excepts.length == 0){
				return;
			}
			var defaultItems:ContextMenuBuiltInItems = m_contextMenu.builtInItems;
			for each(var item:String in excepts){
				defaultItems[item] = true;
			}
		}
		
		/**
		 * 添加自定义菜单项
		 * @param caption			菜单项标题
		 * @param separatorBefore	是否在菜单上方添加分割线
		 * @param listener			事件句柄
		 * @param visible			是否可见
		 */
		public function addCustom(caption:String, separatorBefore:Boolean = false, listener:Function = null, visible:Boolean = true):void {
			var item:ContextMenuItem;
			if (listener != null) {
				item = new ContextMenuItem(caption, separatorBefore, true, visible);
				item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, listener);
			}else {
				item = new ContextMenuItem(caption, separatorBefore, false, visible);
			}
			m_contextMenu.customItems.push(item);
		}
		
		/**
		 * 添加自定义菜单组，所有菜单项共用同一个事件句柄
		 * 通过 e.currentTarget.caption 区分不同的菜单项
		 * @example
		 * protected function onMenuItemSelectedHandler(e:ContextMenuEvent):void {
		 * 		var item:ContextMenuItem = e.currentTarget;
		 * 		switch(item.caption)...
		 * }
		 * @param listener			事件句柄
		 * @param separatorBefore	是否在菜单组上方添加分隔线
		 * @param ...customItems	菜单项标题，可以是一个数组
		 */
		public function addGroup(listener:Function, separatorBefore:Boolean = false, ...customItems):void{
			addCustomItems(listener, customItems, separatorBefore);
		}
		
		/** @private **/
		protected function addCustomItems(listener:Function, customItems:Array, separatorBefore:Boolean = false):void{
			var items:Array = ((customItems.length == 1) && (customItems[0] is Array)) ? customItems[0] : customItems;
			for each(var caption:String in items) {
				addCustom(caption, separatorBefore, listener);
				//保证一个组只添加一个分割线
				if(separatorBefore){
					separatorBefore = false;
				}
			}
		}
	}
}